package com.dbutils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

public class BeanListExample {

	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void main(String[] args) {
//		String url="jdbc:oracle:thin:@192.168.1.2:1521:ora92";
		String url = "jdbc:mysql://localhost/test_java";
		String username="dong";
		String password="xiaodong";
		String sql="select id, name, phone, email from dbutils01_guestbook order by id desc ";
		DbUtils.loadDriver("com.mysql.jdbc.Driver");
		try {
			Connection conn=DriverManager.getConnection(url,username,password);
			QueryRunner qr=new QueryRunner();
			List result=(List)qr.query(conn, sql, new BeanListHandler(GuestBook.class));
			DbUtils.close(conn);
			GuestBook gb=new GuestBook();
			for(int i=0;i<result.size();i++){
				gb=(GuestBook)result.get(i);
				System.out.print("id:"+gb.getId()+"\t");
				System.out.print("name:"+gb.getName()+"\t");
				System.out.print("phone:"+gb.getPhone()+"\t");
				System.out.println("email:"+gb.getEmail());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
